<template>
  <div class="write-wrap">
    <div class="h1">
      <h1><a-input placeholder="请输入标题"  v-model="title"/></h1>
    </div>
    <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editor"
        :defaultConfig="toolbarConfig"
        :mode="mode"
    />
    <Editor
        v-model="html"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="onCreated"
    />
    <div class="publish-setting">
      <a-select
        mode="multiple"
        style="width: 100%"
        placeholder="选择所属的领域"
        v-model="chooseAreas"
      >
        <a-select-option v-for="item in areas" :key="item.id">
          {{ item.name }}
        </a-select-option>
      </a-select>
    </div>
    <div class="bottom-operate">
      <div class="content">
        <div class="left">
          <div class="setting small-hand">
            <div class="item1" v-if="isOnTop" @click="goToTopOrBottom">
              去底部<a-icon type="down" />
            </div>
            <div class="item1" v-else @click="goToTopOrBottom">
              回到顶部<a-icon type="up" />
            </div>
          </div>
        </div>
        <div class="right">
          <div class="preview small-hand" :class="html!=='<p><br></p>' ? 'light-preview':''">预览</div>
          <div class="publish small-hand" :class="html!=='<p><br></p>' ? 'light-publish':''" @click="handlePublish">发布</div>
        </div>
      </div>
    </div>
  </div>
</template>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<script>
import Vue from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import {publish,getAreas,uploadSingleImg} from '@/api/request'
import {getCurrentTime} from '@/assets/js/getTime'
export default Vue.extend({
  name: 'pcWrite',
  components: {
    Editor, Toolbar
  },
  data() {
    return {
      editor: null,
      html: '',
      title:'',
      toolbarConfig: { },
      areas:[],
      chooseAreas:[],
      editorConfig: { 
        placeholder: '请输入内容...' ,
        MENU_CONF:{
          // 图片上传
          uploadImage: {
            async customUpload(file,insertFn){
              let formData = new FormData();
              console.log(file,'file')
              formData.append("file", file);
              let imageUrl=''
              await uploadSingleImg(formData).then(res=>{
                // console.log(res)
                if(res.data.status===0){
                  imageUrl='http://localhost:3007/uploads/'+res.data.url
                }
              })
              insertFn(imageUrl)
            }
          }
        }
      },
      mode: 'default', // or 'simple'
      isOnTop:true
    }
  },
  methods: {
    handleGetAreas(){
      getAreas().then(res=>{
        if(res.data.status===0){
          this.areas=res.data.data
        }
      })
    },
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
    resetContent(){
      this.html='',
      this.title='',
      this.chooseAreas=[],
      this.isOnTop=true
    },
    checkScrollPosition() {
      let scrollTop = window.pageYOffset || document.documentElement.scrollTop; // 获取页面的滚动位置
      let scrollHeight = document.documentElement.scrollHeight; // 获取文档的总高度

      if (scrollTop === 0) {
        this.isOnTop = true; // 页面在顶部
      } else if (scrollTop + window.innerHeight >= scrollHeight) {
        this.isOnTop = false; // 页面在底部
      } else {
        this.isOnTop = true; // 页面不在顶部也不在底部，将变量a设为-1或其他值
      }
    },
    goToTopOrBottom(){
      if(this.isOnTop){
        window.scrollTo(0, document.body.scrollHeight);
      
        this.isOnTop=false
      }
      else{
        window.scrollTo(0, 0);
        this.isOnTop=true
      }
    },
    handlePublish(){
      if(!this.title){
        this.$message.error('标题不能为空！');
        return
      }
      else if(!this.html){
        this.$message.error('内容不能为空！');
        return
      }
      else if(this.chooseAreas.length===0){
        this.$message.error('领域不能为空！');
        return
      }
      const obj={
          title:this.title,
          content:this.html,
          type:'1',
          areas:JSON.stringify(this.chooseAreas),
          time:getCurrentTime()
        }
      console.log('aaa',obj)  
      publish(obj).then(res=>{
        if(res.data.status==0){
          this.$message.success('发布成功！')
          this.resetContent()
        }
      })
    }
  },
  mounted() {
    // 监听滚动事件
    window.addEventListener('scroll', this.checkScrollPosition);
    this.handleGetAreas()
    // 模拟 ajax 请求，异步渲染编辑器
    // setTimeout(() => {
    //   this.html = '<p>模拟 Ajax 异步设置内容 HTML</p>'
    // }, 1500)
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  }
})
</script>

<style scoped lang="less">
.publish-setting{
  margin: 20px 0 60px 0;
}
.write-wrap /deep/ .w-e-text-container{
  min-height:100vh;
}
.write-wrap .bottom-operate{
  height: 60px;
  // width: 1296px;
  // left: -150px;
  position: fixed;
  bottom: 0px;
  background-color: #f0f1f3;
  border-top: 1px solid #cecbcb;
  display: flex;
  justify-content: center;
  .content{
    width: 996px;
    font-size: 20px;
    font-weight: 500;
    color: #8590a6;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left{
      display: flex;
      .setting{
        .item1{
          font-size: 14px;
        }
        i{
          font-size: 14px;
        }
      }
      .word{
        margin-left: 30px;
      }
    }
    .right{
      display: flex;
      .preview{
        padding: 5px 20px;
        background-color: #e5e3e8;
        border-radius: 10px;
      }
      .light-preview{
        background-color: hsl(267, 52%, 96%);
      }
      .publish{
        padding: 5px 20px;
        background-color: #8bb7f8;
        border-radius: 10px;
        color: white;
        margin-left: 20px;
      }
      .light-publish{
        background-color: #094194;
      }
    }
  }
}
</style>
